﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using BLL;

namespace UtilityHelper
{
    public class LogHelper
    {
        public enum LogType
        {
            Error = 0, //错误
            NetSuc,    //联网成功
            NetFail,    //联网失败
            NetUpDown,      //联网上传下载
            NPFail,     //联网平台错误，内部联网平台出现错误
            Normal,
        }

        private static volatile LogHelper instance = null;
        private static object lockHelper = new object();

        public static LogHelper GetSingleLogHelper()
        {
            lock (lockHelper)
            {
                if (instance == null)
                {
                    instance = new LogHelper();
                }
            }

            return instance;
        }


        /// <summary>
        /// 得到错误名称
        /// </summary>
        /// <param name="lType"></param>
        /// <returns></returns>
        public string GetLogName(LogType lType)
        {
            string strRet = "";
            switch (lType)
            {
                case LogType.Error:
                    strRet = "错误";
                    break;
                case LogType.NetFail:
                    strRet = "联网失败";
                    break;
                case LogType.NetSuc:
                    strRet = "联网成功";
                    break;
                case LogType.Normal:
                    strRet = "系统日志";
                    break;
                case LogType.NPFail:
                    strRet = "联网平台错误";
                    break;
            }

            return strRet;
        }

        public bool WriteLog(LOG_INFO logInfo)
        {
            LOG_INFO_BLL bll = new LOG_INFO_BLL();
            return bll.Insert(logInfo);
        }

        public string GetIPAddress(string strHostName)
        {
            string strIPAddress = "";
            var IPs = System.Net.Dns.GetHostAddresses(strHostName);



            for (int i = 0; i < IPs.Length; i++)
            {
                if (IPs[i].AddressFamily.ToString() == "InterNetwork")
                {
                    strIPAddress = IPs.GetValue(i).ToString();
                    break;
                }
            }

            return strIPAddress;
        }
    }
}
